package com.lyy.service.impl;

import com.lyy.dao.IAccountDao;
import com.lyy.domain.Account;
import com.lyy.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AccountServiceImpl implements IAccountService {

    @Autowired
    private IAccountDao accountDao;
    @Override
    public List<Account> findAll() {
        System.out.println("find All account");
       return accountDao.findAll();
    }

    @Override
    public void update(int count) {
        accountDao.update(count);
        System.out.println("update success");
    }

    @Override
    public void transfer(String sourceId, String targetId, float money) {
        //查询初始账户
        Account source = accountDao.findById(sourceId);
        Account target = accountDao.findById(targetId);
        //改变金额
        source.setMoney(source.getMoney()-money);
        target.setMoney(target.getMoney()+money);

        //更新数据库
        accountDao.updateMoney(source);
        int i=1/0;//模拟异常
        accountDao.updateMoney(target);
    }
}
